Smart Pointers: They're Smart, But They're Not Pointers
نویسنده
چکیده
There are numerous times when a C ++ user could beneet from a pointer variant that has more functionality than is provided by the basic, language-deened pointer. For example, type-accurate garbage collection, reference counting, or transparent references to distributed or persistent objects, might be implemented with classes that provide pointer functionality. The C ++ language directly supports one kind of pointer substitute, the smart pointer, in the form of overloadable indirection operators:-> and. In this paper we evaluate how seamlessly smart pointers can replace raw pointers. The ideal is for client code not to care whether it is using raw pointers or smart pointers. For example, if a typedef selects whether raw or smart pointers are used throughout the program, changing the value of the typedef should not introduce syntax errors. Unfortunately, C ++ does not support pointer substitutes well enough to permit seamless integration. This paper presents the desired behavior of smart pointers in terms of the semantics of raw pointers that the smart pointers try to emulate. Then, we describe several ways of implementing smart pointers. For each, we show cases in which the smart-pointers fail to behave like raw pointers. From among the choices, we explain which is the best for emulating the standard pointer conversions. Accessors are similar to smart pointers, but have certain advantages. This paper discusses the diierences between accessors and smart pointers, and shows why our conclusions about type conversion behavior also apply to accessors. Whether a programmer prefers smart pointers or accessors, this paper shows the limitations and recommends an implementation.
منابع مشابه
Montana Smart Pointers: They're Smart, and They're Pointers
The Montana C++ programming environment provides an API interface to the compiler, which allows the compilation process to be extended through programmer-supplied tools. This paper investigates the feasibility of that interface, using smart pointers as an example. Smart pointers are a powerful feature of the C++ language that enable a variety of applications, such as garbage collection, persist...
متن کاملThe Most Important Design Guideline?
T he activity of " design " includes many things, but certainly one of the most important aspects is interface specification. Interfaces determine which aspects of a component are accessible and to whom; they thus determine encapsulation. Interfaces specify what functionality (data, properties , methods, and so forth) is available to clients. Interfaces reflect how a system is broken down into ...
متن کاملThe implementation of generic smart pointers for advanced defensive programming
Smart pointers denote a well-known technique for collective resource ownership, e.g. sharing dynamic object instances, while usually supporting automatic garbage collection based on reference counting. The original method has been retargeted to serve as a generic defensive programming method for ‘exhaustive tracking’ of erroneous pointer use in C++. Automatic bug tracking is supported in a unif...
متن کاملPrecompiling C++ for Garbage Collection
Our research is concerned with compiler-independent, eecient and convenient garbage collection for C++. Most collectors proposed for C++ have either been implemented in a library or in a compiler. As an intermediate step between those two, this paper proposes using precompilation techniques to augment a C++ source program with code to allow mostly type-accurate garbage collection. There are two...
متن کاملImplementation of Binary Search Trees Via Smart Pointers
Study of binary trees has prominent place in the training course of DSA (Data Structures and Algorithms). Their implementation in C++ however is traditionally difficult for students. To a large extent these difficulties are due not so much to the complexity of algorithms as to language complexity in terms of memory management by raw pointers – the programmer must consider too many details to en...
متن کامل